﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MobiLang.

    MobiLang is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace ArgumentsParser
{
    public class ArgsParser
    {
        private struct param
        {
            public string name;
            public bool withValue;
            public param(string name, bool withValue)
            {
                this.name = name;
                this.withValue = withValue;
            }
        }
        private struct arg
        {
            public string name;
            public string value;
            public arg(string name, string value)
            {
                this.name = name;
                this.value = value;
            }
        }
        //
        private int totalArgCount=0;
        //
        private List<param> paramList = new List<param>();
        private List<arg> argList = new List<arg>();
        //
        private string removeFlag(string s)
        {
            if (s.StartsWith("-") || s.StartsWith("/")) return s.Substring(1);
            return s;
        }
        private string removeColon(string s)
        {
            if (s.EndsWith(":")) return s.Substring(0, s.Length - 1);
            return s;
        }
        //
        public ArgsParser(params string[] list)
        {
            paramList.Clear();
            argList.Clear();
            //
            foreach (string s in list)
            {
                if (s.EndsWith(":"))
                {
                    paramList.Add(new param(removeFlag(removeColon(s)), true));
                }
                else
                {
                    paramList.Add(new param(removeFlag(s), false));
                }
            }
            return ;
        }
        //
        public void Parse(string[] args)
        {
            totalArgCount = 0;
            argList.Clear();
            //
            for (;;)
            {
                if (totalArgCount >= args.Length) break;
                //
                string s = args[totalArgCount];
                if(!(s.StartsWith("-") || s.StartsWith("/")))break;
                //
                string t = removeFlag(s);
                var o = Array.Find<param>(paramList.ToArray<param>(), p => p.name.ToLower().Equals(t.ToLower()));
                if (null == o.name)
                {
                    throw new ArgumentException(string.Format("[{0}]",s));
                }
                totalArgCount++;
                if (true == o.withValue)
                {
                    if (totalArgCount >= args.Length)
                    {
                        throw new ArgumentOutOfRangeException(string.Format("[{0}]", s));
                    }
                    argList.Add(new arg(t, args[totalArgCount]));
                    totalArgCount++;
                }
                else
                {
                    argList.Add(new arg(t, null));
                }
                //
            }
            //
            return ;
        }
        //
        public int TotalArgsCount
        {
            get
            {
                return totalArgCount;
            }
        }
        //
        public bool ArgExists(string name)
        {
            //
            name = removeFlag(removeColon(name));
            //
            var o = Array.Find<arg>(argList.ToArray<arg>(), arg => arg.name.ToLower().Equals(name.ToLower()));
            //
            return null != o.name;
        }
        //
        public string ArgValue(string name)
        {
            //
            name = removeFlag(removeColon(name));
            //
            var o = Array.Find<arg>(argList.ToArray<arg>(), arg => arg.name.ToLower().Equals(name.ToLower()));
            //
            return o.value;
        }
        //
    }//class
}//namespace
